package xyz.lwm.lazycat.utility.reflect;

import lombok.Getter;

import java.lang.reflect.Type;

/**
 * example:
 * <pre>
 * TypeRef<List<Long>> ref = new TypeRef<List<Long>>(){};
 * Type argType = ref.getArgumentType();
 * // c = List.class
 * Class c = ref.getGenericClass();
 *
 * Type argType2 = TypeUtil.getTypeArgument(argType, 0);
 * // c2 = Long.class
 * Class c2 = TypeUtil.getClass(argType2);
 *
 * </pre>
 * @param <T>
 */
@Getter
public abstract class TypeRef<T> {

    private final Type argumentType;

    private final Class<T> genericClass;

    @SuppressWarnings("unchecked")
    protected TypeRef() {
        Type superclass = getClass().getGenericSuperclass();
        argumentType = TypeUtil.getTypeArgument(superclass, 0);
        genericClass = (Class<T>) TypeUtil.getClass(argumentType);;
    }

    @Override
    public String toString() {
        return String.valueOf(getArgumentType());
    }

}
